/* 
 * Modbus app for RTU.
 * Copyright (c) 2012  apleilx
 * All rights reserved.
 * no limite
 */

/* ----------------------- Platform includes --------------------------------*/
#define _MBAPP_MODULE_

#include "mbapp.h"
#include "osObjects.h"                                          // RTOS object definitions
#include "tempctr.h"  

uint16_t DataRecevie[32];

uint8_t mbTestErrCnt = 0;
uint8_t mbTestOkCnt = 0;

void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt);

#define mbPORT                       ((LPC_USART_T *) LPC_FLEXCOMM0_BASE)

/*****************************************************************************//*!
* @brief   	Slave device handle.
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/    
void ModbusTask (void  const *p_arg)
{

    //作为主机连接下位模块
    mbxInit(MB_RTU_MASTER, 9600, MB_PAR_EVEN);
    mbxObj.SlaveId=2;
    
    //作为从机连接PC
    mbyInit( MB_RTU_SLAVE, 9600, MB_PAR_EVEN);
    mbyObj.SlaveId=3;
    
    //作为主机连接串口显示屏
    mbzInit(MB_RTU_MASTER, 9600, MB_PAR_EVEN);
    mbzObj.SlaveId=4;
    
    
    //命令配置 端口x读取采样数据
    mbDynaCmdReq(&mbxObj, 2, FUN_CODE_READ_REG, &RegHolding[0], 0, 8, &mbTestCallBack);
	//命令配置 端口x写输出数据至执行模块
    mbDynaCmdReq(&mbxObj, 3, FUN_CODE_READ_REG, (uint16_t*)&TempCtrPar, 16, sizeof(TempCtrPar)/2, &mbTestCallBack);
	
    //写显示数据到 端口y DWIN显示屏
    mbDynaCmdReq(&mbzObj, 2, FUN_CODE_READ_REG, (uint16_t*)&TempCtrPar, 16, sizeof(TempCtrPar)/2, &mbTestCallBack);
    
    for(;;){
        osDelay(10);
        mbMasterPoll(&mbxObj);
        mbSlavePoll(&mbyObj);
        mbMasterPoll(&mbzObj);
 
    }
}


void mbTestCallBack(uint8_t CmdSite, uint8_t ErrSt){
    if(ErrSt)
        mbTestErrCnt++; 
    else
        mbTestOkCnt++;
}

